﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using Microsoft.Phone.Tasks;
using System.Windows.Media.Imaging;
using QuickTasks.FileStoreService;
using LocalSettings;
using QuickTasks.ViewModels.Tables;
using System.Collections.ObjectModel;
namespace QuickTasks
{
    public class QuickMessages
    {
       

        #region events

        public void SaveMessages(string subject, string CC, string recipient, string textmessage, string messageType ,string mid)
        {
             string type = IsolatedSettings.getSettingsStringValue("mtype");
             if (type == "Edit")
             {
                 string DefaultPlace = "";
                 if (IsolatedSettings.getSettingsStringValue("DefaultPlace") != "")
                 {
                     DefaultPlace = IsolatedSettings.getSettingsStringValue("DefaultPlace");
                 }
                 else
                 {
                     DefaultPlace = "1";
                 }

                 IsolatedSettings.saveSettings("type", "");
                 try
                 {
                     using (QuickTaskDataContext context = new QuickTaskDataContext(App.ConnectionString))
                     {
                         IQueryable<messages> messageid = from i in context.message where i.id.ToString() == mid select i;
                         messages msg = messageid.FirstOrDefault();
                         {
                             msg.subject = subject;
                             msg.cc = CC;
                             msg.reciepient = recipient;
                             msg.messagetype = messageType;
                             msg.place = DefaultPlace;
                             msg.message = textmessage;
                         };
                         context.SubmitChanges();
                     }
                   
                 }
                 catch(Exception ex)
                 {
                     Exceptions.SaveOrSendExceptions("Exception in SaveMessages method In QuickMessages file", ex);
                 }
             }
             else
             {
                 try
                 {
                     int tid=0;
                     string DefaultPlace = "";
                     if (IsolatedSettings.getSettingsStringValue("DefaultPlace") != "")
                     {
                         DefaultPlace = IsolatedSettings.getSettingsStringValue("DefaultPlace");
                     }
                     else
                     {
                         DefaultPlace = "1";
                     }

                     using (QuickTaskDataContext context = new QuickTaskDataContext(App.ConnectionString))
                     {
                         var xquery = from a in context.message
                                      orderby a.id descending
                                      select a.id;
                         if (xquery.Count()<= 0)
                         {
                             tid = 1;
                         }
                         else
                         {
                             var xquery1 = from a in context.message
                                          orderby a.id descending
                                          select a;
                             foreach (var query in xquery1)
                             {
                                 tid = Convert.ToInt32(query.id) + 1;
                                 break;
                             }
                         }
                         messages msg = new messages()
                         {
                             id=tid,
                             subject=subject,
                             cc=CC,
                             reciepient=recipient,
                             messagetype=messageType,
                             place=DefaultPlace,
                             message = textmessage
                         };
                         context.message.InsertOnSubmit(msg);
                         context.SubmitChanges();
                     }
                    
                 }
                 catch (Exception ex)
                 {
                     Exceptions.SaveOrSendExceptions("Exception in SaveMessages method In QuickMessages file", ex);
                 }
             }
        }

      

        public List<QuickMessageProp> LoadMessages()
        {
            List<QuickMessageProp> objQuickMessageList = new List<QuickMessageProp>();
            try
            {
                using (QuickTaskDataContext context = new QuickTaskDataContext(App.ConnectionString))
                {
                    var messageList = from m in context.message select m;
                    foreach (var elemessage in messageList)
                    {
                        QuickMessageProp objQuikmes = new QuickMessageProp();
                        objQuikmes.Id = elemessage.id.ToString();
                        objQuikmes.MessageType = elemessage.messagetype;
                        objQuikmes.Subject = elemessage.subject;
                        if (objQuikmes.MessageType == "Email")
                            objQuikmes.ImgMessage = "Images/email.png";
                        else
                            objQuikmes.ImgMessage = "Images/smsphone.png";

                        objQuickMessageList.Add(objQuikmes);
                    }
                }
               
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in LoadMessages method In QuickMessages file", ex);
            }

            return objQuickMessageList;
        }

        public void SendMessage(string CC,string recip,string subject,string message,string type)
        {
            try
            {
                if (type == "Email")
                {

                    string[] Emails = recip.Split(new char[] { ',', ';' });
                    foreach (string Email in Emails)
                    {
                        if (Email != "")
                        {
                            Microsoft.Phone.Tasks.EmailComposeTask emailComposeTask = new Microsoft.Phone.Tasks.EmailComposeTask();
                            emailComposeTask.To = Email;
                            emailComposeTask.Cc =CC;
                            emailComposeTask.Subject = subject;
                            emailComposeTask.Body = message;
                            emailComposeTask.Show();
                          
                        }
                    }
                }
                else
                {

                    string recpats = "";

                    string[] Nos = recip.Split(new char[] { ',', ';' });
                    foreach (string No in Nos)
                    {
                        if (No != "")
                        {
                            if (recpats == "")
                                recpats = No;
                            else

                                recpats = recpats + ";" + No;

                           
                        }
                    }
                    SmsComposeTask sms = new SmsComposeTask();
                    sms.To = recpats;
                    sms.Body = message;
                    sms.Show();
                }
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in SendMessage method In QuickMessages file", ex);
            }
        }

        public ObservableCollection<QuickMessageProp> LoadFavotitePeopleCitiesSameData(string id)
        {
            ObservableCollection<QuickMessageProp> objQuickMessageList = new ObservableCollection<QuickMessageProp>();
            try
            {
                using (QuickTaskDataContext context = new QuickTaskDataContext(App.ConnectionString))
                {
                     var messageList = from i in context.message where i.place == id select i;
                     foreach (var elemessage in messageList)
                     {
                         QuickMessageProp objQuikmes = new QuickMessageProp();
                         objQuikmes.Id = elemessage.id.ToString();
                         objQuikmes.MessageType = elemessage.messagetype;
                         objQuikmes.Subject = elemessage.subject;
                         if (objQuikmes.MessageType == "Email")
                             objQuikmes.ImgMessage = "Images/email.png";
                         else
                             objQuikmes.ImgMessage = "Images/smsphone.png";

                         objQuickMessageList.Add(objQuikmes);
                     }
                }
               
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in LoadFavotitePeopleCitiesSameData method In QuickMessages file", ex);
            }

            return objQuickMessageList;
        #endregion
        }
    }
}
